package tiktok

import (
	"context"
	"fmt"
	"golang.org/x/time/rate"
	"time"
)

type TikTokSDK struct {
	campaignLimiter *rate.Limiter
	adGroupLimiter  *rate.Limiter
	adLimiter       *rate.Limiter
}

func NewTikTokSDK() *TikTokSDK {
	return &TikTokSDK{
		campaignLimiter: rate.NewLimiter(rate.Every(100*time.Millisecond), 5), // 10次/秒
		adGroupLimiter:  rate.NewLimiter(20, 5),                               // 20次/秒
		adLimiter:       rate.NewLimiter(rate.Every(50*time.Millisecond), 10), // 20次/秒
	}
}

func (s *TikTokSDK) CreateCampaign(name string) (string, error) {
	if err := s.campaignLimiter.Wait(context.Background()); err != nil {
		return "", err
	}
	return fmt.Sprintf("tt-campaign-%d", time.Now().Unix()), nil
}

func (s *TikTokSDK) CreateAdGroup(campaignID string) (string, error) {
	if err := s.adGroupLimiter.Wait(context.Background()); err != nil {
		return "", err
	}
	return fmt.Sprintf("tt-adgroup-%d", time.Now().Unix()), nil
}

func (s *TikTokSDK) CreateAd() (string, error) {
	if err := s.adLimiter.Wait(context.Background()); err != nil {
		return "", err
	}
	return fmt.Sprintf("tt-ad-%d", time.Now().Unix()), nil
}
